package sd.zhr.moodcapsule.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;
import sd.zhr.moodcapsule.common.WrapMapper;
import sd.zhr.moodcapsule.common.Wrapper;
import sd.zhr.moodcapsule.pojo.Tag;
import sd.zhr.moodcapsule.service.TagServiceImpl;

import java.util.List;

@RestController
@RequestMapping("/tag")
@CrossOrigin
public class TagController {

    @Autowired
    TagServiceImpl tagServiceimpl;

    @GetMapping("/getAll")
    @Secured({"ROLE_ADMIN", "ROLE_COMMON"})
    public Wrapper<List<Tag>> getAllTag(){
        List<Tag> tagList = tagServiceimpl.findAll();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,tagList);
    }
    @PostMapping("/save")
    @Secured({"ROLE_ADMIN"})
    public Wrapper<Boolean> addTag(@RequestBody Tag tag){
        int result = tagServiceimpl.save(tag);
        boolean flag = result > 0;
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,flag);
    }
    @PutMapping("/update")
    @Secured({"ROLE_ADMIN"})
    public Wrapper<Boolean> updateTag(@RequestBody Tag tag){
        int result = tagServiceimpl.update(tag);
        boolean flag = result > 0;
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,flag);
    }
    @DeleteMapping("/delete/{id}")
    @Secured({"ROLE_ADMIN"})
    public Wrapper<Boolean> deleteTag(@PathVariable Integer id){
        int result = tagServiceimpl.delete(id);
        boolean flag = result > 0;
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,flag);
    }
}
